/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * WordCountAnalysisResultPanel.java
 *
 * Created on 2010-04-18, 17:35:00
 */
package pl.tom.social.analyzer.gui.panel;

import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTable;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import pl.tom.social.common.persist.CsvDumper;
import pl.tom.social.dal.SocialDatabaseFacade;
import pl.tom.social.dal.entity.Profile;

/**
 *
 * @author Tom
 */
public class WordCountAnalysisResultPanel extends javax.swing.JPanel {

	JTable table;

	/** Creates new form WordCountAnalysisResultPanel */
	public WordCountAnalysisResultPanel(JTable table) {
		initComponents();
		this.table = table;
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveToDBButton = new javax.swing.JButton();
        saveToCSVButton = new javax.swing.JButton();
        newProfileButton = new javax.swing.JButton();

        saveToDBButton.setText(org.openide.util.NbBundle.getMessage(WordCountAnalysisResultPanel.class, "WordCountAnalysisResultPanel.saveToDBButton.text")); // NOI18N
        saveToDBButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveToDBButtonActionPerformed(evt);
            }
        });

        saveToCSVButton.setText(org.openide.util.NbBundle.getMessage(WordCountAnalysisResultPanel.class, "WordCountAnalysisResultPanel.saveToCSVButton.text")); // NOI18N
        saveToCSVButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveToCSVButtonActionPerformed(evt);
            }
        });

        newProfileButton.setText(org.openide.util.NbBundle.getMessage(WordCountAnalysisResultPanel.class, "WordCountAnalysisResultPanel.newProfileButton.text")); // NOI18N
        newProfileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newProfileButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(saveToDBButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(saveToCSVButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(newProfileButton))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(saveToDBButton)
                .addComponent(saveToCSVButton)
                .addComponent(newProfileButton))
        );
    }// </editor-fold>//GEN-END:initComponents

	private void saveToDBButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToDBButtonActionPerformed
		// TODO add your handling code here:
	}//GEN-LAST:event_saveToDBButtonActionPerformed

	private void saveToCSVButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveToCSVButtonActionPerformed
		JFrame frame = new JFrame();
		FileDialog dialog = new FileDialog(frame, "Save file", FileDialog.SAVE);
		dialog.setVisible(true);
		String fileName = dialog.getFile();
		if (fileName != null && fileName.length() > 0) {
			CsvDumper.dump(table, fileName);
		}
	}//GEN-LAST:event_saveToCSVButtonActionPerformed

	private void newProfileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newProfileButtonActionPerformed
		final NewProfilePanel panel = new NewProfilePanel();

		int[] rows = table.getSelectedRows();
		StringBuffer buffer = new StringBuffer();
		for (int row : rows) {
			buffer.append(table.getValueAt(row, 0).toString() + " ");
		}
		panel.setQuery(buffer.toString());

		final DialogDescriptor dialog = new DialogDescriptor(panel, "New profile", true, new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == DialogDescriptor.OK_OPTION) {
					SocialDatabaseFacade db = new SocialDatabaseFacade();
					Profile p = new Profile(panel.getQueryName(), panel.getQuery());
					db.persistEntity(p);
				}
			}
		});
		DialogDisplayer.getDefault().notify(dialog);
	}//GEN-LAST:event_newProfileButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton newProfileButton;
    private javax.swing.JButton saveToCSVButton;
    private javax.swing.JButton saveToDBButton;
    // End of variables declaration//GEN-END:variables
}
